1453811c3963e272b2e1e387bde59a6b9da00ee0,GeoTools/process-spatialstatistics/src/main/java/org/geotools/process/spatialstatistics/autocorrelation/GlobalMoranIStatisticOperation.java,GlobalMoranIStatisticOperation,execute,#SimpleFeatureCollection#String#,50

Before Change



    public MoransI execute(SimpleFeatureCollection inputFeatures, String inputField) {
        swMatrix = new SpatialWeightMatrix(getSpatialConceptType(), getStandardizationType());
        swMatrix.distanceBandWidth = this.getDistanceBand();
        swMatrix.buildWeightMatrix(inputFeatures, inputField, this.getDistanceType());

        // """Calculate Moran's Index and Z Score."""
        double dSumWC = 0.0; // # summation of weighted co-variance (dWij * dCij)
        double dSumW = 0.0; // # summation of all weights (dWij)
        double dM2 = 0.0;
        double dM4 = 0.0;
        double dSumS1 = 0.0;
        double dSumS2 = 0.0;

        // Calculate sample mean.
        double n = swMatrix.Events.size() * 1.0;
        double dZMean = swMatrix.dZSum / n;

        for (SpatialEvent curE : swMatrix.Events) {
            double dWijS2Sum = 0.0;
            double dWjiS2Sum = 0.0;
            double dZiDeviation = curE.weight - dZMean; // # Calculate deviation from mean
            dM2 += Math.pow(dZiDeviation, 2.0);
            dM4 += Math.pow(dZiDeviation, 4.0);

            // # Look for i's local neighbors
            for (SpatialEvent destE : swMatrix.Events) {
                if (curE.oid == destE.oid)
                    continue;

                double dZjDeviation = destE.weight - dZMean;
                double dCij = dZiDeviation * dZjDeviation; // # Calculate ij co-variance

                // # Calculate the weight (dWij)
                double dWij = 0.0;
                double dWji = 0.0;

                if (this.getSpatialConceptType() == SpatialConcept.POLYGONCONTIGUITY) {
                    dWij = 0.0;
                    // if (destE is neighbor ) dWij = 1.0;
                    dWji = dWij;
                } else {
                    dWij = swMatrix.getWeight(curE, destE);
                    dWji = dWij;
                }

                if (getStandardizationType() == StandardizationMethod.ROW) {
                    dWij = swMatrix.standardizeWeight(curE, dWij);
                    dWji = swMatrix.standardizeWeight(destE, dWji);
                }

                // # Create sums needed to calculate Moran's I
                dSumWC += dWij * dCij;
                dSumW += dWij;
                dWijS2Sum += dWij;
                dWjiS2Sum += dWji;
                dSumS1 += Math.pow(dWij + dWji, 2.0);
            }

            dSumS2 += Math.pow(dWijS2Sum + dWjiS2Sum, 2.0);
        }
        dSumS1 = 0.5 * dSumS1;

        // # we need a few more working variables:
        dM2 = (dM2 * 1.0) / n; // # standard deviation
        dM4 = (dM4 * 1.0) / n;

        double dB2 = dM4 / (dM2 * dM2); // # sample kurtosis
        double dExpected = -1.0 / (n - 1.0); // # Expected Moran's I

        if (dSumW <= 0.0)
            return new MoransI();

        // Finally, we can calculate Moran's I and its significance (Z Score).
        // This Z Score is based on the calculated RANDOMIZATION null hypothesis.

After Change



    public MoransI execute(SimpleFeatureCollection inputFeatures, String inputField) {
        swMatrix = new SpatialWeightMatrix(getSpatialConceptType(), getStandardizationType());
        swMatrix.setDistanceMethod(getDistanceType());
        swMatrix.setDistanceBandWidth(getDistanceBand());
        swMatrix.buildWeightMatrix(inputFeatures, inputField);

        double dSumWC = 0.0;
        double dSumW = 0.0;
        double dM2 = 0.0;
        double dM4 = 0.0;
        double dSumS1 = 0.0;
        double dSumS2 = 0.0;

        // Calculate sample mean.
        double n = swMatrix.getEvents().size() * 1.0;
        double dZMean = swMatrix.dZSum / n;

        for (SpatialEvent curE : swMatrix.getEvents()) {
            double dWijS2Sum = 0.0;
            double dWjiS2Sum = 0.0;
            double dZiDeviation = curE.weight - dZMean;
            dM2 += Math.pow(dZiDeviation, 2.0);
            dM4 += Math.pow(dZiDeviation, 4.0);

            for (SpatialEvent destE : swMatrix.getEvents()) {
                if (curE.oid == destE.oid) {
                    continue;
                }

                double dZjDeviation = destE.weight - dZMean;
                double dCij = dZiDeviation * dZjDeviation;

                // Calculate the weight (dWij)
                double dWij = swMatrix.getWeight(curE, destE);
                double dWji = dWij;

                if (getStandardizationType() == StandardizationMethod.Row) {
                    dWij = swMatrix.standardizeWeight(curE, dWij);
                    dWji = swMatrix.standardizeWeight(destE, dWji);
                }

                // Create sums needed to calculate Moran's I
                dSumWC += dWij * dCij;
                dSumW += dWij;
                dWijS2Sum += dWij;
                dWjiS2Sum += dWji;
                dSumS1 += Math.pow(dWij + dWji, 2.0);
            }

            dSumS2 += Math.pow(dWijS2Sum + dWjiS2Sum, 2.0);
        }
        dSumS1 = 0.5 * dSumS1;

        // we need a few more working variables:
        dM2 = (dM2 * 1.0) / n; // # standard deviation
        dM4 = (dM4 * 1.0) / n;

        double dB2 = dM4 / (dM2 * dM2); // sample kurtosis
        double dExpected = -1.0 / (n - 1.0); // Expected Moran's I

        if (dSumW <= 0.0) {
            MoransI moransI = new MoransI(0d, dExpected, 0d);
            moransI.setConceptualization(getSpatialConceptType());
            moransI.setDistanceMethod(getDistanceType());
            moransI.setRowStandardization(getStandardizationType());
            moransI.setDistanceThreshold(swMatrix.getDistanceBandWidth());
            return moransI;
        }